package linkedlist;

//                                          模拟实现链表

//表示一个链表的节点
class Node{
    //表示节点保存的值
    public String value;
    //表示下一个节点
    public Node next;

    public Node(String value){
        this.value = value;
        this.next = null;
    }
}

public class MyLinkedList {
    //需要一个头节点表示出来
    //head == null 表示链表为空
    public Node head = null;

    //头插
    public void addFirst(String value){

        Node newNode = new Node(value);
        //1.让新节点的 next 指向原来的 头节点
        newNode.next = head;
        //2. 让头节点指向新的节点
        head = newNode;
    }



    public String toString(){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for (Node cur = head; cur != null; cur =  cur.next){
            stringBuilder.append(cur.value);
            if (cur.next != null){
                stringBuilder.append(", ");
            }
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }





    public static void test1(){
        MyLinkedList list = new MyLinkedList();
        list.addFirst("a");
        list.addFirst("b");
        list.addFirst("c");
        list.addFirst("d");
        System.out.println(list);
    }
    public static void main(String[] args) {
        test1();
    }
}
